{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8b7fa1fc-5c21-48ac-aa64-22e2dc95a6d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T08:57:22.246135Z",
     "iopub.status.busy": "2022-03-27T08:57:22.245757Z",
     "iopub.status.idle": "2022-03-27T08:57:22.386182Z",
     "shell.execute_reply": "2022-03-27T08:57:22.385509Z",
     "shell.execute_reply.started": "2022-03-27T08:57:22.246112Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d414ed7-c130-488b-84b7-d8fe433c002d",
   "metadata": {},
   "source": [
    "1. 通用文件操作：保存-savetxt() & 读取-loadtxt()\n",
    "    1. 一般用于操作类似csv的文本文件\n",
    "    2. 可以设置header\n",
    "    3. 不能存储3维以上的数组\n",
    "2. 专用文件操作：保存-save() & 读取-load()\n",
    "    1. 一般用于存取非文本类型的文件\n",
    "    2. 不可设置header\n",
    "    3. 可以存取3维以上的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ceb65b9d-9959-40b4-bf2f-b35d0339c11d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T08:57:25.271443Z",
     "iopub.status.busy": "2022-03-27T08:57:25.271190Z",
     "iopub.status.idle": "2022-03-27T08:57:25.277013Z",
     "shell.execute_reply": "2022-03-27T08:57:25.276240Z",
     "shell.execute_reply.started": "2022-03-27T08:57:25.271417Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function savetxt in module numpy:\n",
      "\n",
      "savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\\n', header='', footer='', comments='# ', encoding=None)\n",
      "    Save an array to a text file.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    fname : filename or file handle\n",
      "        If the filename ends in ``.gz``, the file is automatically saved in\n",
      "        compressed gzip format.  `loadtxt` understands gzipped files\n",
      "        transparently.\n",
      "    X : 1D or 2D array_like\n",
      "        Data to be saved to a text file.\n",
      "    fmt : str or sequence of strs, optional\n",
      "        A single format (%10.5f), a sequence of formats, or a\n",
      "        multi-format string, e.g. 'Iteration %d -- %10.5f', in which\n",
      "        case `delimiter` is ignored. For complex `X`, the legal options\n",
      "        for `fmt` are:\n",
      "    \n",
      "        * a single specifier, `fmt='%.4e'`, resulting in numbers formatted\n",
      "          like `' (%s+%sj)' % (fmt, fmt)`\n",
      "        * a full string specifying every real and imaginary part, e.g.\n",
      "          `' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'` for 3 columns\n",
      "        * a list of specifiers, one per column - in this case, the real\n",
      "          and imaginary part must have separate specifiers,\n",
      "          e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns\n",
      "    delimiter : str, optional\n",
      "        String or character separating columns.\n",
      "    newline : str, optional\n",
      "        String or character separating lines.\n",
      "    \n",
      "        .. versionadded:: 1.5.0\n",
      "    header : str, optional\n",
      "        String that will be written at the beginning of the file.\n",
      "    \n",
      "        .. versionadded:: 1.7.0\n",
      "    footer : str, optional\n",
      "        String that will be written at the end of the file.\n",
      "    \n",
      "        .. versionadded:: 1.7.0\n",
      "    comments : str, optional\n",
      "        String that will be prepended to the ``header`` and ``footer`` strings,\n",
      "        to mark them as comments. Default: '# ',  as expected by e.g.\n",
      "        ``numpy.loadtxt``.\n",
      "    \n",
      "        .. versionadded:: 1.7.0\n",
      "    encoding : {None, str}, optional\n",
      "        Encoding used to encode the outputfile. Does not apply to output\n",
      "        streams. If the encoding is something other than 'bytes' or 'latin1'\n",
      "        you will not be able to load the file in NumPy versions < 1.14. Default\n",
      "        is 'latin1'.\n",
      "    \n",
      "        .. versionadded:: 1.14.0\n",
      "    \n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    save : Save an array to a binary file in NumPy ``.npy`` format\n",
      "    savez : Save several arrays into an uncompressed ``.npz`` archive\n",
      "    savez_compressed : Save several arrays into a compressed ``.npz`` archive\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    Further explanation of the `fmt` parameter\n",
      "    (``%[flag]width[.precision]specifier``):\n",
      "    \n",
      "    flags:\n",
      "        ``-`` : left justify\n",
      "    \n",
      "        ``+`` : Forces to precede result with + or -.\n",
      "    \n",
      "        ``0`` : Left pad the number with zeros instead of space (see width).\n",
      "    \n",
      "    width:\n",
      "        Minimum number of characters to be printed. The value is not truncated\n",
      "        if it has more characters.\n",
      "    \n",
      "    precision:\n",
      "        - For integer specifiers (eg. ``d,i,o,x``), the minimum number of\n",
      "          digits.\n",
      "        - For ``e, E`` and ``f`` specifiers, the number of digits to print\n",
      "          after the decimal point.\n",
      "        - For ``g`` and ``G``, the maximum number of significant digits.\n",
      "        - For ``s``, the maximum number of characters.\n",
      "    \n",
      "    specifiers:\n",
      "        ``c`` : character\n",
      "    \n",
      "        ``d`` or ``i`` : signed decimal integer\n",
      "    \n",
      "        ``e`` or ``E`` : scientific notation with ``e`` or ``E``.\n",
      "    \n",
      "        ``f`` : decimal floating point\n",
      "    \n",
      "        ``g,G`` : use the shorter of ``e,E`` or ``f``\n",
      "    \n",
      "        ``o`` : signed octal\n",
      "    \n",
      "        ``s`` : string of characters\n",
      "    \n",
      "        ``u`` : unsigned decimal integer\n",
      "    \n",
      "        ``x,X`` : unsigned hexadecimal integer\n",
      "    \n",
      "    This explanation of ``fmt`` is not complete, for an exhaustive\n",
      "    specification see [1]_.\n",
      "    \n",
      "    References\n",
      "    ----------\n",
      "    .. [1] `Format Specification Mini-Language\n",
      "           <https://docs.python.org/library/string.html#format-specification-mini-language>`_,\n",
      "           Python Documentation.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> x = y = z = np.arange(0.0,5.0,1.0)\n",
      "    >>> np.savetxt('test.out', x, delimiter=',')   # X is an array\n",
      "    >>> np.savetxt('test.out', (x,y,z))   # x,y,z equal sized 1D arrays\n",
      "    >>> np.savetxt('test.out', x, fmt='%1.4e')   # use exponential notation\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.savetxt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "664be2ef-39c5-4be3-a209-3c13ad211635",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T09:27:48.311012Z",
     "iopub.status.busy": "2022-03-27T09:27:48.310722Z",
     "iopub.status.idle": "2022-03-27T09:27:48.318254Z",
     "shell.execute_reply": "2022-03-27T09:27:48.317327Z",
     "shell.execute_reply.started": "2022-03-27T09:27:48.310983Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生成绩：\n",
      "[[68 35 91]\n",
      " [18 29 88]\n",
      " [80 86 35]\n",
      " [25 38  4]\n",
      " [91 45 41]\n",
      " [61 42 71]\n",
      " [55 98 16]\n",
      " [21  1 47]\n",
      " [37 92 83]\n",
      " [17 65 57]\n",
      " [35 13 74]\n",
      " [ 8 69  8]\n",
      " [47 94 61]\n",
      " [43 36 22]\n",
      " [ 1 87 12]\n",
      " [14 46 60]\n",
      " [ 6 51 42]\n",
      " [72 53 51]\n",
      " [42 54 29]\n",
      " [68 43 14]]\n"
     ]
    }
   ],
   "source": [
    "# 创建数组scores，存放20个学生语数英三科的成绩\n",
    "scores=np.random.randint(0, 100, size=(20, 3))\n",
    "print(\"学生成绩：\\n{}\".format(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "50fd60dc-fb56-4c0c-a5a3-064a6d127fbc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T15:26:28.322697Z",
     "iopub.status.busy": "2022-03-27T15:26:28.322420Z",
     "iopub.status.idle": "2022-03-27T15:26:28.326695Z",
     "shell.execute_reply": "2022-03-27T15:26:28.326092Z",
     "shell.execute_reply.started": "2022-03-27T15:26:28.322670Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将scores保存为csv文件\n",
    "np.savetxt(\"scores.csv\", scores, delimiter=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a893c245-ec35-42de-be6f-403c80c84bb6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T15:37:44.001723Z",
     "iopub.status.busy": "2022-03-27T15:37:44.001460Z",
     "iopub.status.idle": "2022-03-27T15:37:44.005835Z",
     "shell.execute_reply": "2022-03-27T15:37:44.005108Z",
     "shell.execute_reply.started": "2022-03-27T15:37:44.001696Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将scores保存为csv文件，指定header\n",
    "np.savetxt(\"scores.csv\", scores, delimiter=\",\", header=\"语文,数学,英语\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "31585476-6d0f-40a1-8e43-cb10b861702b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T15:47:25.242397Z",
     "iopub.status.busy": "2022-03-27T15:47:25.242123Z",
     "iopub.status.idle": "2022-03-27T15:47:25.248028Z",
     "shell.execute_reply": "2022-03-27T15:47:25.246977Z",
     "shell.execute_reply.started": "2022-03-27T15:47:25.242370Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将scores保存为csv文件，指定header，设定comments为空字符\n",
    "np.savetxt(\"scores.csv\", scores, delimiter=\",\", header=\"语文,数学,英语\", comments=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8af0334c-e809-4cdf-b03c-c1d82dea1574",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T16:08:07.746488Z",
     "iopub.status.busy": "2022-03-27T16:08:07.746237Z",
     "iopub.status.idle": "2022-03-27T16:08:07.751192Z",
     "shell.execute_reply": "2022-03-27T16:08:07.750374Z",
     "shell.execute_reply.started": "2022-03-27T16:08:07.746461Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将scores保存为csv文件，指定header，设定comments为空字符，设定数据的保存格式\n",
    "np.savetxt(\"scores.csv\", scores, delimiter=\",\", header=\"语文,数学,英语\", comments=\"\", fmt=\"%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f9db86e7-fb77-4e3d-a5b5-d9bdd7179d27",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T16:14:15.505473Z",
     "iopub.status.busy": "2022-03-27T16:14:15.505227Z",
     "iopub.status.idle": "2022-03-27T16:14:15.510186Z",
     "shell.execute_reply": "2022-03-27T16:14:15.509087Z",
     "shell.execute_reply.started": "2022-03-27T16:14:15.505451Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function loadtxt in module numpy:\n",
      "\n",
      "loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None, *, like=None)\n",
      "    Load data from a text file.\n",
      "    \n",
      "    Each row in the text file must have the same number of values.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    fname : file, str, pathlib.Path, list of str, generator\n",
      "        File, filename, list, or generator to read.  If the filename\n",
      "        extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note\n",
      "        that generators must return bytes or strings. The strings\n",
      "        in a list or produced by a generator are treated as lines.\n",
      "    dtype : data-type, optional\n",
      "        Data-type of the resulting array; default: float.  If this is a\n",
      "        structured data-type, the resulting array will be 1-dimensional, and\n",
      "        each row will be interpreted as an element of the array.  In this\n",
      "        case, the number of columns used must match the number of fields in\n",
      "        the data-type.\n",
      "    comments : str or sequence of str, optional\n",
      "        The characters or list of characters used to indicate the start of a\n",
      "        comment. None implies no comments. For backwards compatibility, byte\n",
      "        strings will be decoded as 'latin1'. The default is '#'.\n",
      "    delimiter : str, optional\n",
      "        The string used to separate values. For backwards compatibility, byte\n",
      "        strings will be decoded as 'latin1'. The default is whitespace.\n",
      "    converters : dict, optional\n",
      "        A dictionary mapping column number to a function that will parse the\n",
      "        column string into the desired value.  E.g., if column 0 is a date\n",
      "        string: ``converters = {0: datestr2num}``.  Converters can also be\n",
      "        used to provide a default value for missing data (but see also\n",
      "        `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``.\n",
      "        Default: None.\n",
      "    skiprows : int, optional\n",
      "        Skip the first `skiprows` lines, including comments; default: 0.\n",
      "    usecols : int or sequence, optional\n",
      "        Which columns to read, with 0 being the first. For example,\n",
      "        ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns.\n",
      "        The default, None, results in all columns being read.\n",
      "    \n",
      "        .. versionchanged:: 1.11.0\n",
      "            When a single column has to be read it is possible to use\n",
      "            an integer instead of a tuple. E.g ``usecols = 3`` reads the\n",
      "            fourth column the same way as ``usecols = (3,)`` would.\n",
      "    unpack : bool, optional\n",
      "        If True, the returned array is transposed, so that arguments may be\n",
      "        unpacked using ``x, y, z = loadtxt(...)``.  When used with a\n",
      "        structured data-type, arrays are returned for each field.\n",
      "        Default is False.\n",
      "    ndmin : int, optional\n",
      "        The returned array will have at least `ndmin` dimensions.\n",
      "        Otherwise mono-dimensional axes will be squeezed.\n",
      "        Legal values: 0 (default), 1 or 2.\n",
      "    \n",
      "        .. versionadded:: 1.6.0\n",
      "    encoding : str, optional\n",
      "        Encoding used to decode the inputfile. Does not apply to input streams.\n",
      "        The special value 'bytes' enables backward compatibility workarounds\n",
      "        that ensures you receive byte arrays as results if possible and passes\n",
      "        'latin1' encoded strings to converters. Override this value to receive\n",
      "        unicode arrays and pass strings as input to converters.  If set to None\n",
      "        the system default is used. The default value is 'bytes'.\n",
      "    \n",
      "        .. versionadded:: 1.14.0\n",
      "    max_rows : int, optional\n",
      "        Read `max_rows` lines of content after `skiprows` lines. The default\n",
      "        is to read all the lines.\n",
      "    \n",
      "        .. versionadded:: 1.16.0\n",
      "    like : array_like\n",
      "        Reference object to allow the creation of arrays which are not\n",
      "        NumPy arrays. If an array-like passed in as ``like`` supports\n",
      "        the ``__array_function__`` protocol, the result will be defined\n",
      "        by it. In this case, it ensures the creation of an array object\n",
      "        compatible with that passed in via this argument.\n",
      "    \n",
      "        .. versionadded:: 1.20.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray\n",
      "        Data read from the text file.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    load, fromstring, fromregex\n",
      "    genfromtxt : Load data with missing values handled as specified.\n",
      "    scipy.io.loadmat : reads MATLAB data files\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    This function aims to be a fast reader for simply formatted files.  The\n",
      "    `genfromtxt` function provides more sophisticated handling of, e.g.,\n",
      "    lines with missing values.\n",
      "    \n",
      "    .. versionadded:: 1.10.0\n",
      "    \n",
      "    The strings produced by the Python float.hex method can be used as\n",
      "    input for floats.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> from io import StringIO   # StringIO behaves like a file object\n",
      "    >>> c = StringIO(\"0 1\\n2 3\")\n",
      "    >>> np.loadtxt(c)\n",
      "    array([[0., 1.],\n",
      "           [2., 3.]])\n",
      "    \n",
      "    >>> d = StringIO(\"M 21 72\\nF 35 58\")\n",
      "    >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),\n",
      "    ...                      'formats': ('S1', 'i4', 'f4')})\n",
      "    array([(b'M', 21, 72.), (b'F', 35, 58.)],\n",
      "          dtype=[('gender', 'S1'), ('age', '<i4'), ('weight', '<f4')])\n",
      "    \n",
      "    >>> c = StringIO(\"1,0,2\\n3,0,4\")\n",
      "    >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)\n",
      "    >>> x\n",
      "    array([1., 3.])\n",
      "    >>> y\n",
      "    array([2., 4.])\n",
      "    \n",
      "    This example shows how `converters` can be used to convert a field\n",
      "    with a trailing minus sign into a negative number.\n",
      "    \n",
      "    >>> s = StringIO('10.01 31.25-\\n19.22 64.31\\n17.57- 63.94')\n",
      "    >>> def conv(fld):\n",
      "    ...     return -float(fld[:-1]) if fld.endswith(b'-') else float(fld)\n",
      "    ...\n",
      "    >>> np.loadtxt(s, converters={0: conv, 1: conv})\n",
      "    array([[ 10.01, -31.25],\n",
      "           [ 19.22,  64.31],\n",
      "           [-17.57,  63.94]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.loadtxt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d04ac7ea-9d99-49db-b496-9145e6b4a059",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T16:28:28.173797Z",
     "iopub.status.busy": "2022-03-27T16:28:28.173549Z",
     "iopub.status.idle": "2022-03-27T16:28:28.588999Z",
     "shell.execute_reply": "2022-03-27T16:28:28.588069Z",
     "shell.execute_reply.started": "2022-03-27T16:28:28.173770Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_12120/1907374040.py:1: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  readScores = np.loadtxt(\"scores.csv\", dtype=np.int, delimiter=\",\")\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '语文'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m readScores \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloadtxt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mscores.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdelimiter\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m,\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:1163\u001b[0m, in \u001b[0;36mloadtxt\u001b[0;34m(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows, like)\u001b[0m\n\u001b[1;32m   1159\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1160\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWrong number of columns at line \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mlineno\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m   1161\u001b[0m     \u001b[38;5;66;03m# Convert each value according to its column, then pack it\u001b[39;00m\n\u001b[1;32m   1162\u001b[0m     \u001b[38;5;66;03m# according to the dtype's nesting, and store it.\u001b[39;00m\n\u001b[0;32m-> 1163\u001b[0m     chunk\u001b[38;5;241m.\u001b[39mappend(packer(\u001b[43mconvert_row\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwords\u001b[49m\u001b[43m)\u001b[49m))\n\u001b[1;32m   1164\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m chunk:  \u001b[38;5;66;03m# The islice is empty, i.e. we're done.\u001b[39;00m\n\u001b[1;32m   1165\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:1142\u001b[0m, in \u001b[0;36mloadtxt.<locals>.convert_row\u001b[0;34m(vals, _conv)\u001b[0m\n\u001b[1;32m   1141\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconvert_row\u001b[39m(vals, _conv\u001b[38;5;241m=\u001b[39mconverters[\u001b[38;5;241m0\u001b[39m]):\n\u001b[0;32m-> 1142\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m [\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mmap\u001b[39m(_conv, vals)]\n",
      "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '语文'"
     ]
    }
   ],
   "source": [
    "# 读取scores.csv文件\n",
    "readScores = np.loadtxt(\"scores.csv\", dtype=np.int, delimiter=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2d428bd2-d44f-4653-8752-dcec7062086a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T16:37:01.152034Z",
     "iopub.status.busy": "2022-03-27T16:37:01.151795Z",
     "iopub.status.idle": "2022-03-27T16:37:01.157351Z",
     "shell.execute_reply": "2022-03-27T16:37:01.156367Z",
     "shell.execute_reply.started": "2022-03-27T16:37:01.152011Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[68 35 91]\n",
      " [18 29 88]\n",
      " [80 86 35]\n",
      " [25 38  4]\n",
      " [91 45 41]\n",
      " [61 42 71]\n",
      " [55 98 16]\n",
      " [21  1 47]\n",
      " [37 92 83]\n",
      " [17 65 57]\n",
      " [35 13 74]\n",
      " [ 8 69  8]\n",
      " [47 94 61]\n",
      " [43 36 22]\n",
      " [ 1 87 12]\n",
      " [14 46 60]\n",
      " [ 6 51 42]\n",
      " [72 53 51]\n",
      " [42 54 29]\n",
      " [68 43 14]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_12120/772440105.py:1: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  readScores = np.loadtxt(\"scores.csv\", dtype=np.int, delimiter=\",\", skiprows=1)\n"
     ]
    }
   ],
   "source": [
    "# 读取scores.csv文件，忽略第1行\n",
    "readScores = np.loadtxt(\"scores.csv\", dtype=np.int, delimiter=\",\", skiprows=1)\n",
    "print(readScores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d0f7cf40-456e-43d5-8489-d3b14cde0edf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T17:13:03.836724Z",
     "iopub.status.busy": "2022-03-27T17:13:03.836471Z",
     "iopub.status.idle": "2022-03-27T17:13:03.840784Z",
     "shell.execute_reply": "2022-03-27T17:13:03.840014Z",
     "shell.execute_reply.started": "2022-03-27T17:13:03.836698Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 以npy格式保存scores到硬盘\n",
    "np.save(\"scores\", scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "218f5ce5-ac72-4c07-a56d-221494f34429",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T17:19:35.894335Z",
     "iopub.status.busy": "2022-03-27T17:19:35.894070Z",
     "iopub.status.idle": "2022-03-27T17:19:35.899164Z",
     "shell.execute_reply": "2022-03-27T17:19:35.898446Z",
     "shell.execute_reply.started": "2022-03-27T17:19:35.894307Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[68 35 91]\n",
      " [18 29 88]\n",
      " [80 86 35]\n",
      " [25 38  4]\n",
      " [91 45 41]\n",
      " [61 42 71]\n",
      " [55 98 16]\n",
      " [21  1 47]\n",
      " [37 92 83]\n",
      " [17 65 57]\n",
      " [35 13 74]\n",
      " [ 8 69  8]\n",
      " [47 94 61]\n",
      " [43 36 22]\n",
      " [ 1 87 12]\n",
      " [14 46 60]\n",
      " [ 6 51 42]\n",
      " [72 53 51]\n",
      " [42 54 29]\n",
      " [68 43 14]]\n"
     ]
    }
   ],
   "source": [
    "# 读取scores.npy文件\n",
    "readNpy = np.load(\"scores.npy\")\n",
    "print(readNpy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7acb718c-520a-4152-9bab-153f9bea28e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T17:35:12.456038Z",
     "iopub.status.busy": "2022-03-27T17:35:12.455794Z",
     "iopub.status.idle": "2022-03-27T17:35:12.515131Z",
     "shell.execute_reply": "2022-03-27T17:35:12.513976Z",
     "shell.execute_reply.started": "2022-03-27T17:35:12.456014Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 3 1 5 1 5]\n",
      "  [9 0 6 8 6 8]\n",
      "  [5 1 1 5 0 4]\n",
      "  [3 0 0 5 7 3]\n",
      "  [5 8 9 9 4 3]]\n",
      "\n",
      " [[8 6 6 7 2 5]\n",
      "  [9 5 6 3 4 1]\n",
      "  [9 4 6 9 6 8]\n",
      "  [0 0 3 6 9 2]\n",
      "  [7 2 2 7 3 6]]\n",
      "\n",
      " [[1 4 8 3 2 9]\n",
      "  [4 2 7 2 9 3]\n",
      "  [2 2 1 9 6 6]\n",
      "  [9 5 1 1 3 1]\n",
      "  [6 9 9 9 0 7]]\n",
      "\n",
      " [[0 1 0 0 2 8]\n",
      "  [2 3 5 5 0 0]\n",
      "  [5 3 8 7 2 4]\n",
      "  [7 7 4 0 8 8]\n",
      "  [1 0 5 4 3 7]]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Expected 1D or 2D array, got 3D array instead",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m testArr\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39mrandom\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m10\u001b[39m, size\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m6\u001b[39m))\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(testArr)\n\u001b[0;32m----> 3\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msavetxt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtest.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43mtestArr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdelimiter\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m,\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m<__array_function__ internals>:180\u001b[0m, in \u001b[0;36msavetxt\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/numpy/lib/npyio.py:1397\u001b[0m, in \u001b[0;36msavetxt\u001b[0;34m(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)\u001b[0m\n\u001b[1;32m   1395\u001b[0m \u001b[38;5;66;03m# Handle 1-dimensional arrays\u001b[39;00m\n\u001b[1;32m   1396\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m X\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m X\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m2\u001b[39m:\n\u001b[0;32m-> 1397\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1398\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExpected 1D or 2D array, got \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124mD array instead\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m X\u001b[38;5;241m.\u001b[39mndim)\n\u001b[1;32m   1399\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m X\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m   1400\u001b[0m     \u001b[38;5;66;03m# Common case -- 1d array of numbers\u001b[39;00m\n\u001b[1;32m   1401\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m X\u001b[38;5;241m.\u001b[39mdtype\u001b[38;5;241m.\u001b[39mnames \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "\u001b[0;31mValueError\u001b[0m: Expected 1D or 2D array, got 3D array instead"
     ]
    }
   ],
   "source": [
    "# 用savetxt存储三维数组\n",
    "testArr=np.random.randint(0, 10, size=(4,5,6))\n",
    "print(testArr)\n",
    "np.savetxt(\"test.csv\",testArr, delimiter=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8865fdf9-b11f-45b5-86d1-292933aa9831",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-27T17:39:20.092687Z",
     "iopub.status.busy": "2022-03-27T17:39:20.092430Z",
     "iopub.status.idle": "2022-03-27T17:39:20.097968Z",
     "shell.execute_reply": "2022-03-27T17:39:20.097157Z",
     "shell.execute_reply.started": "2022-03-27T17:39:20.092661Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[9 7 7 0 5 5]\n",
      "  [6 8 0 9 7 6]\n",
      "  [2 6 5 6 2 1]\n",
      "  [5 8 5 8 8 9]\n",
      "  [0 7 3 5 3 8]]\n",
      "\n",
      " [[0 8 8 4 1 6]\n",
      "  [5 5 5 1 3 9]\n",
      "  [6 2 7 8 8 3]\n",
      "  [9 9 8 9 1 9]\n",
      "  [4 5 8 7 4 4]]\n",
      "\n",
      " [[7 8 8 0 7 3]\n",
      "  [9 2 2 5 8 2]\n",
      "  [8 6 1 3 1 0]\n",
      "  [6 8 2 1 1 0]\n",
      "  [8 0 9 7 5 8]]\n",
      "\n",
      " [[7 4 1 3 3 2]\n",
      "  [6 9 1 6 7 2]\n",
      "  [6 8 4 5 0 7]\n",
      "  [2 2 9 0 2 5]\n",
      "  [5 7 5 7 5 1]]]\n"
     ]
    }
   ],
   "source": [
    "# 用save存储三维数组\n",
    "testArr=np.random.randint(0, 10, size=(4,5,6))\n",
    "print(testArr)\n",
    "np.save(\"test\",testArr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
